using ADODB;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace CatalogCopy
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class frmCatalogCopy : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ComboBox cmbSites;
		private System.Windows.Forms.Label lblSites;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.ListBox lstCatalogs;
		private System.Windows.Forms.Label lblCatalogs;
		private System.Windows.Forms.TextBox txtNewCatalog;

		private string selectedSite = null;
		private string selectedCatalog = null;
		private System.Windows.Forms.Button btnDelete;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmCatalogCopy()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.cmbSites = new System.Windows.Forms.ComboBox();
			this.lblSites = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.lstCatalogs = new System.Windows.Forms.ListBox();
			this.lblCatalogs = new System.Windows.Forms.Label();
			this.txtNewCatalog = new System.Windows.Forms.TextBox();
			this.btnDelete = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// cmbSites
			// 
			this.cmbSites.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cmbSites.Location = new System.Drawing.Point(128, 8);
			this.cmbSites.Name = "cmbSites";
			this.cmbSites.Size = new System.Drawing.Size(232, 21);
			this.cmbSites.TabIndex = 0;
			this.cmbSites.SelectedIndexChanged += new System.EventHandler(this.cmbSites_SelectedIndexChanged);
			// 
			// lblSites
			// 
			this.lblSites.Location = new System.Drawing.Point(24, 8);
			this.lblSites.Name = "lblSites";
			this.lblSites.Size = new System.Drawing.Size(96, 16);
			this.lblSites.TabIndex = 1;
			this.lblSites.Text = "Commerce Sites:";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(8, 208);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(112, 23);
			this.button1.TabIndex = 2;
			this.button1.Text = "Copy Catalog As...";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// lstCatalogs
			// 
			this.lstCatalogs.Location = new System.Drawing.Point(128, 40);
			this.lstCatalogs.Name = "lstCatalogs";
			this.lstCatalogs.Size = new System.Drawing.Size(232, 160);
			this.lstCatalogs.TabIndex = 3;
			this.lstCatalogs.SelectedIndexChanged += new System.EventHandler(this.lstCatalogs_SelectedIndexChanged);
			// 
			// lblCatalogs
			// 
			this.lblCatalogs.Location = new System.Drawing.Point(64, 40);
			this.lblCatalogs.Name = "lblCatalogs";
			this.lblCatalogs.Size = new System.Drawing.Size(56, 23);
			this.lblCatalogs.TabIndex = 4;
			this.lblCatalogs.Text = "Catalogs:";
			// 
			// txtNewCatalog
			// 
			this.txtNewCatalog.Location = new System.Drawing.Point(128, 208);
			this.txtNewCatalog.Name = "txtNewCatalog";
			this.txtNewCatalog.Size = new System.Drawing.Size(232, 20);
			this.txtNewCatalog.TabIndex = 5;
			this.txtNewCatalog.Text = "<New Catalog Name>";
			// 
			// btnDelete
			// 
			this.btnDelete.Location = new System.Drawing.Point(8, 64);
			this.btnDelete.Name = "btnDelete";
			this.btnDelete.Size = new System.Drawing.Size(112, 23);
			this.btnDelete.TabIndex = 6;
			this.btnDelete.Text = "Delete Catalog";
			this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
			// 
			// frmCatalogCopy
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(368, 238);
			this.Controls.Add(this.btnDelete);
			this.Controls.Add(this.txtNewCatalog);
			this.Controls.Add(this.lblCatalogs);
			this.Controls.Add(this.lstCatalogs);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.lblSites);
			this.Controls.Add(this.cmbSites);
			this.Name = "frmCatalogCopy";
			this.Text = "Copy Catalog As...";
			this.Load += new System.EventHandler(this.frmCatalogCopy_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmCatalogCopy());
		}

		private void frmCatalogCopy_Load(object sender, System.EventArgs e)
		{
			// clear out the combo site
			cmbSites.Items.Clear();

			// get a list of site
			_Recordset rs = Util.GetCommerceSites();

			foreach(Field fld in rs.Fields)
			{
				cmbSites.Items.Add(fld.Value);
			}

			// go to the first site if we have one
			if(rs.RecordCount > 0)
				cmbSites.SelectedIndex = 0;
		}

		private void cmbSites_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			// set the current selected site
			selectedSite = cmbSites.SelectedItem.ToString();

			SetCatalogs(selectedSite);
		}

		private void SetCatalogs(string sitename)
		{
			// clear out the list
			lstCatalogs.Items.Clear();

			// get a list of catalogs in site
			_Recordset rs = Util.GetCatalogs(cmbSites.SelectedItem.ToString());

			if(rs!=null)
			{
				while(!rs.EOF)
				{
					lstCatalogs.Items.Add(rs.Fields["CatalogName"].Value.ToString());
					rs.MoveNext();
				}
			}
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;
			// copy the catalog as...
			bool retValu = Util.CreateCatalog(selectedCatalog , txtNewCatalog.Text, selectedSite);

			// reset the catalogs
			SetCatalogs(selectedSite);

			Cursor.Current = Cursors.Default;
		}

		private void lstCatalogs_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			// set the current selected catalog
			selectedCatalog = lstCatalogs.SelectedItem.ToString();
		}

		private void btnDelete_Click(object sender, System.EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;

			// delete selected catalog
			Util.DeleteCatalog(selectedSite, selectedCatalog);

			// reset the catalogs
			SetCatalogs(selectedSite);

			Cursor.Current = Cursors.Default;
		}
	}
}
